package algorithms4g

import "fmt"

func init() {
	nums := []int{1, 3, 4, 5, 6, 8, 10}
	target := 8
	res2 := binarySearch1(nums, target)
	fmt.Println("二分查找", res2)
}

/**
二分查找
*/
func binarySearch1(nums []int, target int) int {
	left, right := 0, len(nums)-1
	for left <= right {
		mid := left + (right-left)/2
		if nums[mid] == target {
			return mid
		} else if nums[mid] < target {
			left = mid + 1
		} else {
			right = mid - 1
		}
	}
	return -1
}
